import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { STColumn, STComponent } from '@delon/abc';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { CostManageCostEditComponent } from '../cost-edit/cost-edit.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'app-cost-manage-cost-list',
  templateUrl: './cost-list.component.html',
})
export class CostManageCostListComponent implements OnInit {
 
  @ViewChild('st', { static: false }) st: TablePageComponent;
  addFlag:boolean = this.aclService.canAbility("cost-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("cost-manage/edit");
  loading = false;
  selectData: any[];
  config: SPage = {
    columns: [
      {
        index: 'areaName',
        title: '小区',
        filter: {
          placeholder: '请输入小区名称',
          type: 'string'
        }
      },
      {
        index: 'areaParentAllName',
        width:400,
        title: '地址',
      },
      {
        index: 'itemName',
        title: '项目名称',
      },
      {
        index:"itemType",
        title:"项目类型",
        fFn(item, columu) {
          var arr = item.itemTypeList
          if(arr == null){
            return ""
          }
          console.log(arr)
          var list=[]
          for(let item of arr){
            list.push("&nbsp;&nbsp;&nbsp;&nbsp;"+item.name)
          }
          return list.join(",")
        },
      },
      {
        index:"openType",
        title:"开通类型",
        type: ColumnType.tag,
        tag: {
          1:{text: '整体开通'},
          2:{text: '单个开通'},
        },
      },
      // {
      //   index:"employTime",
      //   title:"使用时长(天)",
      // },
      {
        index: 'monthlyRent',
        title: '月租(元)',
      },
      // {
      //   index: 'remark',
      //   title: '备注',
      //   /*filter: {
      //     placeholder: '请输入数据权限类型',
      //     type: 'string'
      //   }*/
      // },
      /*{
        index: 'createTime',
        title: '添加时间',
        filter: {
          placeholder: 'YYYY-MM-dd 00:00',
          type: 'string'
        }
      }*/
      {
        index: '',
        title: '操作',
        width:200,
        fixed:'right',
        buttons:[
          {
            text:'新增',
            iif:()=>{
              return this.addFlag;
            },
            click:(_record)=>{
              this.editDic(0,_record);
            }
          },
          {
            text:'编辑',
            iif: (data: STData) => {
              return data.itemId != null && this.editFlag;
            },
            click:(_record)=>{
              this.editDic(1, _record);
            }
          },
          {
            text:'删除',
            iif: (data: STData) => {
              return data.itemId != null;
            },
            click:(_record)=>{
              this.delItem(_record);
            }
          },
        ]
      },
    ],
    // showCheckBox: true,
    reqUrl:this.api.pay.arealist.url,
    // editBtn: [
    //   { title: '新增' },
    //   { 
      
    //    title: '编辑',
    //    iif: (data: STData) => {
    //     return data.itemId != null
    //   }
    //  },
    //   //{ title: '删除' }
    // ]
  }

  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  ngOnInit() {
  }

  dataProcess(data:STData[]){
    console.log(data);
    for(let item of data){
      if(item.itemId != null){
        item.monthlyRent= parseInt(item.monthlyRent) / 100;
      }
    }
    return data;
  }

 


  // 编辑功能开始
  editDic(type: 0 | 1 = 0, _param?: object): void {
    let title="";
    if(type == 0){
        title="费用项新增";
    }else if(type == 1){
        title="费用项编辑";
    }
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: CostManageCostEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  operate(e: any): void {
    if (e.title === '编辑') {
      this.editDic(1, e.data);
    }else if(e.title === '删除'){
      this.modal.confirm({
        nzTitle: '您确定要删除这些数据吗',
        nzOnOk: () => {
          const body = [];
          // this.selectData.forEach(item => {
          //   body.push({id: item.id})
          // })
          this.http.put(this.api.user.delete.url,body).subscribe(res => {
            if (res.status === '200') {
              this.message.success('删除成功');
              this.st.reload();
            }
          })
        }
      })
    }else if(e.title === '新增'){
      this.editDic(0,e.data);
    }
  }

  delItem(record){
    this.modal.confirm({
      nzTitle: '是否删除?',
      nzOnOk: () => {
        this.http.get("/core/pay/item/delete?id="+record.itemId).subscribe(res => {
          if (res.status === '200') {
            this.message.success('删除成功');
            this.st.reload();
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }
 
  _console(e: any): void {
    this.selectData = e;
  }




}
